import Vue from 'vue';
import VueRouter from 'vue-router';
import Homepage from '@/components/homepage/Homepage.vue';
import Login from '@/components/Login.vue';
import Sign from '@/components/homepage/Sign.vue';
import Forgotpassword from '@/components/homepage/Forgotpassword.vue';
import Sort from '@/components/homepage/Sort.vue';
import Lndividual from '@/components/lndividual/Lndividual.vue';
import Overview from '@/components/lndividual/Overview.vue';
import EditProfileForm from '@/components/lndividual/EditProfileForm.vue';
import Address from '@/components/lndividual/Address.vue';
import Merchants from '@/components/lndividual/Merchants.vue';
import Supplier from '@/components/lndividual/Supplier.vue';
import Shopping from '@/components/shopping/Shopping.vue';
import Bear from '@/components/shopping/Bear.vue';
import ProductDetails from '@/components/homepage/ProductDetails.vue';



import Kuang from '@/components/Shanghuguanli/Kuang.vue';
import Merchant from '@/components/Shanghuguanli/Merchant.vue';
import Weihu from '@/components/Shanghuguanli/Weihu.vue';
import ShoHuoDindan from '@/components/Shanghuguanli/ShoHuoDindan.vue';
import Deliveryorder from '@/components/Shanghuguanli/Deliveryorder.vue';
import Receivingorder from '@/components/Shanghuguanli/Receivingorder.vue';
import Returnorder from '@/components/Shanghuguanli/Returnorder.vue';
import Statistic from '@/components/Shanghuguanli/Statistic.vue';

import BianKuang from '@/components/GongYingShang/BianKuang.vue';
import Product from '@/components/GongYingShang/Product.vue';
import Shangpinweihu from '@/components/GongYingShang/Shangpinweihu.vue';
import ChakanDindan from '@/components/GongYingShang/ChakanDindan.vue';
import ZiliaoWeihu from '@/components/GongYingShang/ZiliaoWeihu.vue';



const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
 
 
 
// replace
const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace= function replace(location) {
  return originalReplace.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const router=new VueRouter({
	routes: [
	    // 重定向
	   { path: '/', redirect: '/login' },
	//    { path: '/', redirect: '/homepage' },
	    // 登录组件
	    
		{path:'/login',component:Login},
		//主页
		{ path: '/homepage', component:Homepage },
		//注册
		{ path: '/sign', component:Sign },
		//忘记密码
		{ path: '/forgotpassword', component:Forgotpassword },
		//商品分类页面
		{ path: '/sort', component:Sort },
		//商品详情页面
		{ path: '/productdetails', component:ProductDetails },
		//购物车
		{ path: '/Shopping', component:Shopping },
		//结算
		{ path: '/Bear', component: Bear },
		//个人中心组件
		{ path: '/Lndividual', component:Lndividual,
			children:[
				//我的订单
				{ path: '/Overview', component: Overview },
				//个人信息
				{ path: '/EditProfileForm', component: EditProfileForm },
				//提货地址
				{ path: '/Address', component: Address },
				//商户入驻
				{ path: '/Merchants', component: Merchants },
				//供应商招募
				{ path: '/Supplier', component: Supplier },
				{ path: '/Merchant', component: Merchant },
				{ path: '/Weihu', component: Weihu },
				{ path: '/ShoHuoDindan', component: ShoHuoDindan },
				{ path: '/Deliveryorder', component: Deliveryorder },
				{ path: '/Receivingorder', component: Receivingorder },
				{ path: '/Returnorder', component: Returnorder },
				{ path: '/Statistic', component: Statistic },
				{ path: '/Product', component: Product },
				{ path: '/Shangpinweihu', component: Shangpinweihu },
				{ path: '/ChakanDindan',component: ChakanDindan},
				{ path: '/ZiliaoWeihu',component: ZiliaoWeihu},
				{path: '/Kuang', component: Kuang,},
				// { path: '/Merchant', component: Merchant },
				// 		{ path: '/Weihu', component: Weihu },
				// 		{ path: '/ShoHuoDindan', component: ShoHuoDindan },
				// 		{ path: '/Deliveryorder', component: Deliveryorder },
				// 		{ path: '/Receivingorder', component: Receivingorder },
				// 		{ path: '/Returnorder', component: Returnorder },
				// 		{ path: '/Statistic', component: Statistic },
				// {path: '/BianKuang', component: BianKuang,},
				// { path: '/Product', component: Product },
				// 		{ path: '/Shangpinweihu', component: Shangpinweihu },
				// 		{ path: '/ChakanDindan',component: ChakanDindan},
				// 		{ path: '/ZiliaoWeihu',component: ZiliaoWeihu},
				{path: '/Kuang', component: Kuang,
					children:[
						{ path: '/Merchant', component: Merchant },
						{ path: '/Weihu', component: Weihu },
						{ path: '/ShoHuoDindan', component: ShoHuoDindan },
						{ path: '/Deliveryorder', component: Deliveryorder },
						{ path: '/Receivingorder', component: Receivingorder },
						{ path: '/Returnorder', component: Returnorder },
						{ path: '/Statistic', component: Statistic },


					]


				},
				{path: '/BianKuang', component: BianKuang,
					children:[
						{ path: '/Product', component: Product },
						{ path: '/Shangpinweihu', component: Shangpinweihu },
						{ path: '/ChakanDindan',component: ChakanDindan},
						{ path: '/ZiliaoWeihu',component: ZiliaoWeihu},


					]


				}
			]
		 },


	]
})

//全局导航守卫
router.beforeEach((to, from, next)=>{
   // 如果访问的路由是/login, 不判断直接放行
    if(to.path === '/login' || to.path==='/forgotpassword' || to.path==='/sign')
        return next()
    // 如果访问的路由不是/login, 判断是否有token
    // 从sessionStorage中获取token
    const token = window.sessionStorage.getItem('token')
    if(!token || token=="undefined")
        return next('/login')
    next()
})

export default router